package com.bootdo.common.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.common.config.Constant;
import com.bootdo.common.domain.TaskDO;
import com.bootdo.common.service.DictService;
import com.smart.common.utils.PageUtils;
import com.smart.common.utils.Query;
import com.smart.common.utils.R;
import com.smart.domain.DictDO;
import com.smart.enums.ReturnCodeEnum;
import com.smart.enums.SaveOrUpdateEnum;

/**
 * 字典表
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2017-09-29 18:28:07
 */

@Controller
@RequestMapping("/common/dict")
public class DictController extends BaseController {

	private DictService dictService;

	/**
	 * 进入列表页面
	 * @return
	 */
	@GetMapping()
	@RequiresPermissions("common:dict:dict")
	String dict() {
		return "common/dict/dict";
	}

	/**
	 * 分页
	 * @param params
	 * @return
	 */
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("common:dict:dict")
	public PageUtils list(@RequestParam Map<String, Object> params) {
		List<DictDO> list = dictService.list(params);
		Query query = new Query(params);
		int total = dictService.count(query);
		PageUtils pageUtils = new PageUtils(list, total);
		return pageUtils;
	}

	/**
	 * 新增页面
	 * @return
	 */
	@GetMapping("/add")
	@RequiresPermissions("common:dict:add")
	String add() {
		return "common/dict/add";
	}

	/**
	 * 新增页面
	 * @param id
	 * @param model
	 * @return
	 */
	@GetMapping("/edit/{id}")
	@RequiresPermissions("common:dict:edit")
	String edit(@PathVariable("id") Long id, Model model) {
		DictDO dictDO = dictService.get(id);
		model.addAttribute("dict", dictDO);
		return "common/dict/edit";
	}

	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("common:dict:add")
	public R save(DictDO dict) {
		if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
			return R.error(1, "演示系统不允许修改,完整体验请部署程序");
		}
		//新增校验重复
		R r = dictService.checkDuplicate(dict, SaveOrUpdateEnum.SAVE.getCode());
		if (!r.get("code").equals(ReturnCodeEnum.SUCCESS.getCode())){
			return r;
		}
		dictService.save(dict);
		return R.ok();
	}

	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("common:dict:edit")
	public R update(DictDO dict) {
		if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
			return R.error(1, "演示系统不允许修改,完整体验请部署程序");
		}
		//编辑校验重复
		R r = dictService.checkDuplicate(dict, SaveOrUpdateEnum.UPDATE.getCode());
		if (!r.get("code").equals(ReturnCodeEnum.SUCCESS.getCode())){
			return r;
		}
		dictService.update(dict);
		return R.ok();
	}

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ResponseBody
	@RequiresPermissions("common:dict:remove")
	public R remove(Long id) {
		if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
			return R.error(1, "演示系统不允许修改,完整体验请部署程序");
		}
		dictService.remove(id);
		return R.ok();
	}

	/**
	 * 删除
	 */
	@PostMapping("/batchRemove")
	@ResponseBody
	@RequiresPermissions("common:dict:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids) {
		if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
			return R.error(1, "演示系统不允许修改,完整体验请部署程序");
		}
		dictService.batchRemove(ids);
		return  R.ok();
	}

	@GetMapping("/type")
	@ResponseBody
	public List<DictDO> listType() {
	    return dictService.listType();
	}

	// 类别已经指定增加
	@GetMapping("/add/{type}/{description}")
	@RequiresPermissions("common:dict:add")
	String addD(Model model, @PathVariable("type") String type, @PathVariable("description") String description) {
		model.addAttribute("type", type);
		model.addAttribute("description", description);
		return "common/dict/add";
	}

	@ResponseBody
	@GetMapping("/list/{type}")
	public List<DictDO> listByType(@PathVariable("type") String type) {
		List<DictDO> list = dictService.listByType(type);
		return list;
	}
	/**
	 * 
	 *  一次查询多个字典数据
	 * @param keys
	 * @return
	 * @version: 2018年12月20日 上午9:31:13
	 * @author: zhouwc
	 */
	//TODO 目前还未验证，用到之后同意修改
	@ResponseBody
	@GetMapping("/listdictbykeys")
	public Map<String,List<DictDO>> listDicts(@RequestParam(value = "dictKeys[]") String[] keys) {
		if(keys == null || keys.length == 0) {
			return null;
		}
		Map<String, List<DictDO>> listDicts = dictService.listDicts(keys);
		return listDicts;
	}
}
